package clientView;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import object.Employee;
import uploadServer.CheckImg;
import uploadServer.ImagePreview;
import uploadServer.ImgFilter;
import uploadServer.UploadImg;
import utility.StringUtil;
import clientMain.Loading;
import clientMain.Main;
import clientUtilGUI.KeyValue;
import clientUtilGUI.UtilityGUI;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
import com.toedter.calendar.JDateChooser;

import database.Paging;

public class Employee_Add extends JPanel implements ActionListener {
	
	//	Design	
	private JPanel panelHeader;
	private JScrollPane scrollMain;
	private JPanel panelMain;
	private GridBagLayout gbl_panelMain;
	private JPanel panelTitle1;
	private GridBagConstraints gbc_panelTitle1;
	private JPanel panelForm1;
	private GridBagConstraints gbc_panelForm1;
	private JLabel lblFName;
	private JTextField txtFName;
	private JLabel lblLName;
	private JTextField txtLName;
	private JLabel lblUpload;
	private JTextField txtPathImg;
	private JFileChooser fileChooser;
	private JButton btnChooserFile;
	private JLabel lblSex;
	private JRadioButton rdbtnNam;
	private JRadioButton rdbtnNu;
	private JLabel lblBirthday;
	private JDateChooser dateBirthday;
	private JLabel lblCMTND;
	private JTextField txtCMTND;
	private JLabel lblHomeTown;
	private JTextField txtHomeTown;
	private JComboBox cbbProvinceHT;
	private JComboBox cbbDistrictHT;
	private JLabel lblCurrent;
	private JTextField txtCurrent;
	private JComboBox cbbProvinceCur;
	private JComboBox cbbDistrictCur;
	private JLabel lblPhone;
	private JTextField txtPhone;
	private JLabel lblMobile;
	private JTextField txtMobile;
	private JLabel lblEmail;
	private JTextField txtEmail;
	private JLabel lblProvinceHT;
	private JLabel lblDistictHT;
	private JLabel lblProvinceCur;
	private JLabel lblDistrictCur;
	private JPanel panelTitle2;
	private GridBagConstraints gbc_panelTitle2;
	private JPanel panelForm2;
	private GridBagConstraints gbc_panelForm2;
	private JLabel lblDepartment;
	private JComboBox cbbDep;
	private JLabel lblEmType;
	private JComboBox cbbEmT;
	private JLabel lblPostion;
	private JComboBox cbbPosition;
	private JLabel lblDateStart;
	private JDateChooser dateStart;
	private JLabel lblContract;
	private JComboBox cbbContract;
	private JLabel lblDiplomas;
	private JComboBox cbbDiplomas;
	private JLabel lblSpec;
	private JComboBox cbbSpec;
	private JPanel panelBtn;
	private JButton btnSave;
	private JButton btnCancel;
	private JLabel lblNote;
	private JLabel lbl;
	private ButtonGroup groupSex;
	
	public Format formatter;
	public Date maxDateBirthday = Main.fullday;
	public String minStrBirthday = "01/01/1930";
	public int maxDayStart = 10;
	private String minStrDateStart = "01/01/2000";
		
	//	Upload
	private String fileNameChooser;
	public static String fileName = "";
	private File selectedFile;
	private String resultSelectFile;
	private InputStream is;
	private ImageInputStream iis;
	
	private int provinceCurId;
	private int districtCurId;
	private int provinceHTId;
	private int districtHTId;

	private Vector<JTextField> groupTxt = new Vector<JTextField>();
	
	public Employee_Add() {
		this.setLayout(new BorderLayout(0, 0));
		
		panelHeader = UtilityGUI.headerForm("Hồ Sơ Nhân Viên");
		this.add(panelHeader, BorderLayout.NORTH);
		
		panelMain = new JPanel();
		gbl_panelMain = new GridBagLayout();
		gbl_panelMain.columnWidths = new int[]{0};
		gbl_panelMain.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
		gbl_panelMain.columnWeights = new double[]{1.0};
		gbl_panelMain.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
		panelMain.setLayout(gbl_panelMain);
		
		//	panelTitle1
		panelTitle1 = UtilityGUI.titleForm("Thông Tin Cá Nhân");
		gbc_panelTitle1 = new GridBagConstraints();
		gbc_panelTitle1.insets = new Insets(0, 0, 0, 0);
		gbc_panelTitle1.fill = GridBagConstraints.BOTH;
		gbc_panelTitle1.gridx = 0;
		gbc_panelTitle1.gridy = 0;
		panelMain.add(panelTitle1, gbc_panelTitle1);
		
		//	Form1
		panelForm1 = new JPanel();
		gbc_panelForm1 = new GridBagConstraints();
		gbc_panelForm1.fill = GridBagConstraints.BOTH;
		gbc_panelForm1.insets = new Insets(5, 10, 5, 10);
		gbc_panelForm1.gridx = 0;
		gbc_panelForm1.gridy = 1;
		panelMain.add(panelForm1, gbc_panelForm1);
		panelForm1.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("62dlu"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("62dlu"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("95dlu"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("left:default"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("85dlu"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("18dlu"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("18dlu"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("18dlu"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("18dlu"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("18dlu"),}));
		
		lblFName = new JLabel("Họ và tên đệm :");
		lblFName.setFont(UtilityGUI.fontLblForm);
		panelForm1.add(lblFName, "2, 2, left, center");
		txtFName = new JTextField();
		txtFName.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtFName.setText(StringUtil.removeUnwantedSpace(txtFName.getText()));
			}
		});
		panelForm1.add(txtFName, "4, 2, 3, 1, fill, center");
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(Color.RED);
		panelForm1.add(lbl, "8, 2");
		
		lblLName = new JLabel("Tên :");
		lblLName.setFont(UtilityGUI.fontLblForm);
		panelForm1.add(lblLName, "12, 2, left, center");
		txtLName = new JTextField();
		txtLName.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtLName.setText(StringUtil.removeUnwantedSpace(txtLName.getText()));
			}
		});
		panelForm1.add(txtLName, "14, 2, 3, 1, fill, center");
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(Color.RED);
		panelForm1.add(lbl, "18, 2");
		
		lblUpload = new JLabel("Ảnh :");
		lblUpload.setFont(UtilityGUI.fontLblForm);
		panelForm1.add(lblUpload, "22, 2, left, center");
		txtPathImg = new JTextField();
		txtPathImg.setEditable(false);
		panelForm1.add(txtPathImg, "24, 2, 3, 1, fill, center");
		btnChooserFile = new JButton(UtilityGUI.btnSelectFile);
		btnChooserFile.setFont(UtilityGUI.fontBtnForm);
		btnChooserFile.addActionListener(this);
		btnChooserFile.setActionCommand("SelectFile");
		panelForm1.add(btnChooserFile, "27, 2, 4, 1, left, center");
		
		lblSex = new JLabel("Giới tính :");
		lblSex.setFont(UtilityGUI.fontLblForm);
		panelForm1.add(lblSex, "2, 4");
		rdbtnNam = new JRadioButton("Nam", true);
		rdbtnNam.addActionListener(this);
		panelForm1.add(rdbtnNam, "4, 4, left, center");
		rdbtnNu = new JRadioButton("Nữ", false);
		rdbtnNu.addActionListener(this);
		panelForm1.add(rdbtnNu, "6, 4, left, center");
		groupSex = new ButtonGroup();
		groupSex.add(rdbtnNam);
		groupSex.add(rdbtnNu);
		
		lblBirthday = new JLabel("Ngày sinh :");
		lblBirthday.setFont(UtilityGUI.fontLblForm);
		panelForm1.add(lblBirthday, "12, 4, left, center");
		dateBirthday = new JDateChooser();
		dateBirthday.setLocale(new Locale("vi"));
		dateBirthday.setMaxSelectableDate(maxDateBirthday);
		formatter = new SimpleDateFormat(Main.strFormatDate);
		Date minDateBirhtday = null;
		try {
			minDateBirhtday = (Date) formatter.parseObject(minStrBirthday);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		dateBirthday.setMinSelectableDate(minDateBirhtday);
		dateBirthday.setDateFormatString(Main.strFormatDate);
		dateBirthday.setFont(new Font("Tahoma", Font.BOLD, 11));
		panelForm1.add(dateBirthday, "14, 4, 3, 1, fill, center");
		lbl = new JLabel("*");
		lbl.setForeground(Color.RED);
		lbl.setFont(new Font("Tahoma", Font.ITALIC, 11));
		panelForm1.add(lbl, "18, 4");
		
		lblCMTND = new JLabel("CMTND :");
		lblCMTND.setFont(UtilityGUI.fontLblForm);
		panelForm1.add(lblCMTND, "22, 4, left, center");
		txtCMTND = new JTextField();
		txtCMTND.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtCMTND.setText(StringUtil.removeUnwantedSpace(txtCMTND.getText()));
			}
		});
		panelForm1.add(txtCMTND, "24, 4, 3, 1, fill, center");
		lbl = new JLabel("*");
		lbl.setForeground(Color.RED);
		lbl.setFont(UtilityGUI.fontNote);
		panelForm1.add(lbl, "28, 4");
		
		lblHomeTown = new JLabel("Quê quán :");
		lblHomeTown.setFont(UtilityGUI.fontLblForm);
		panelForm1.add(lblHomeTown, "2, 6, left, center");
		txtHomeTown = new JTextField();
		txtHomeTown.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtHomeTown.setText(StringUtil.removeUnwantedSpace(txtHomeTown.getText()));
			}
		});
		panelForm1.add(txtHomeTown, "4, 6, 3, 1, fill, center");
		
		lblProvinceHT = new JLabel("Tỉnh(Thành phố) :");
		lblProvinceHT.setFont(UtilityGUI.fontLblForm);
		panelForm1.add(lblProvinceHT, "12, 6, left, center");
		cbbProvinceHT = UtilityGUI.getComboBox("Province", 0,"");
		cbbProvinceHT.addActionListener(this);
		panelForm1.add(cbbProvinceHT, "14, 6, 3, 1, fill, center");
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(Color.RED);
		panelForm1.add(lbl, "18, 6");
		
		lblDistictHT = new JLabel("Quận(Huyện) :");
		lblDistictHT.setFont(UtilityGUI.fontLblForm);
		panelForm1.add(lblDistictHT, "22, 6, left, center");
		cbbDistrictHT = new JComboBox();
		cbbDistrictHT.addItem("-- Chọn Quận(huyện) --");
		cbbDistrictHT.setEnabled(false);
		cbbDistrictHT.setFont(new Font("Tahoma", Font.BOLD, 11));
		panelForm1.add(cbbDistrictHT, "24, 6, 3, 1, fill, center");
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(Color.RED);
		panelForm1.add(lbl, "28, 6");
		
		lblCurrent = new JLabel("Địa chỉ hiện tại :");
		lblCurrent.setFont(UtilityGUI.fontLblForm);
		panelForm1.add(lblCurrent, "2, 8, left, center");
		txtCurrent = new JTextField();
		txtCurrent.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtCurrent.setText(StringUtil.removeUnwantedSpace(txtCurrent.getText()));
			}
		});
		panelForm1.add(txtCurrent, "4, 8, 3, 1, fill, center");
		
		lbl = new JLabel("*");
		lbl.setForeground(Color.RED);
		lbl.setFont(new Font("Tahoma", Font.ITALIC, 11));
		panelForm1.add(lbl, "8, 8");
		
		lblProvinceCur = new JLabel("Tỉnh(Thành phố) :");
		lblProvinceCur.setFont(UtilityGUI.fontLblForm);
		panelForm1.add(lblProvinceCur, "12, 8, left, center");
		cbbProvinceCur = UtilityGUI.getComboBox("Province", 0,"");
		cbbProvinceCur.addActionListener(this);
		panelForm1.add(cbbProvinceCur, "14, 8, 3, 1, fill, default");
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(Color.RED);
		panelForm1.add(lbl, "18, 8");
		
		lblDistrictCur = new JLabel("Quận(Huyện) :");
		lblDistrictCur.setFont(UtilityGUI.fontLblForm);
		panelForm1.add(lblDistrictCur, "22, 8, left, center");
		cbbDistrictCur = new JComboBox();
		cbbDistrictCur.addItem("-- Chọn Quận(huyện) --");
		cbbDistrictCur.setFont(new Font("Tahoma", Font.BOLD, 11));
		cbbDistrictCur.setEnabled(false);
		panelForm1.add(cbbDistrictCur, "24, 8, 3, 1, fill, default");
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(Color.RED);
		panelForm1.add(lbl, "28, 8");
		
		lblPhone = new JLabel("ĐT cố định :");
		lblPhone.setFont(UtilityGUI.fontLblForm);
		panelForm1.add(lblPhone, "2, 10, left, center");
		txtPhone = new JTextField();
		txtPhone.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtPhone.setText(StringUtil.removeUnwantedSpace(txtPhone.getText()));
			}
		});
		panelForm1.add(txtPhone, "4, 10, 3, 1, fill, center");
		
		lblMobile = new JLabel("ĐTDĐ :");
		lblMobile.setFont(UtilityGUI.fontLblForm);
		panelForm1.add(lblMobile, "12, 10, left, center");
		txtMobile = new JTextField();
		txtMobile.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtMobile.setText(StringUtil.removeUnwantedSpace(txtMobile.getText()));
			}
		});
		panelForm1.add(txtMobile, "14, 10, 3, 1, fill, center");
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(Color.RED);
		panelForm1.add(lbl, "18, 10");
		
		lblEmail = new JLabel("Email :");
		lblEmail.setFont(UtilityGUI.fontLblForm);
		panelForm1.add(lblEmail, "22, 10, left, center");
		txtEmail = new JTextField();
		txtEmail.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtEmail.setText(StringUtil.removeUnwantedSpace(txtEmail.getText()));
			}
		});
		panelForm1.add(txtEmail, "24, 10, 3, 1, fill, center");
		
		//	panelTitle2	
		panelTitle2 = UtilityGUI.titleForm("Thông Tin Sự Nghiệp");
		gbc_panelTitle2 = new GridBagConstraints();
		gbc_panelTitle2.insets = new Insets(0, 0, 0, 0);
		gbc_panelTitle2.fill = GridBagConstraints.BOTH;
		gbc_panelTitle2.gridx = 0;
		gbc_panelTitle2.gridy = 2;
		panelMain.add(panelTitle2, gbc_panelTitle2);
		
		//	Form2
		panelForm2 = new JPanel();
		gbc_panelForm2 = new GridBagConstraints();
		gbc_panelForm2.fill = GridBagConstraints.BOTH;
		gbc_panelForm2.insets = new Insets(5, 10, 5, 10);
		gbc_panelForm2.gridx = 0;
		gbc_panelForm2.gridy = 3;
		panelMain.add(panelForm2, gbc_panelForm2);
		panelForm2.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("95dlu"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("97dlu"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("18dlu"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("18dlu"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("18dlu"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("18dlu"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("18dlu"),}));
		
		lblDepartment = new JLabel("Phòng Ban :");
		lblDepartment.setFont(UtilityGUI.fontLblForm);
		panelForm2.add(lblDepartment, "2, 2, left, center");
		cbbDep = UtilityGUI.getComboBox("Department", 0,"");
		panelForm2.add(cbbDep, "4, 2, 13, 1, fill, center");
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(Color.RED);
		panelForm2.add(lbl, "18, 2");
		
		lblEmType = new JLabel("Nghiệp vụ :");
		lblEmType.setFont(UtilityGUI.fontLblForm);
		panelForm2.add(lblEmType, "2, 4, left, center");
		cbbEmT = UtilityGUI.getComboBox("EmployeeType", 0,"");
		panelForm2.add(cbbEmT, "4, 4, 3, 1, fill, center");
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(Color.RED);
		panelForm2.add(lbl, "8, 4");
		
		lblPostion = new JLabel("Chức vụ :");
		lblPostion.setFont(UtilityGUI.fontLblForm);
		panelForm2.add(lblPostion, "12, 4, left, center");
		cbbPosition = UtilityGUI.getComboBox("Position", 0,"");
		panelForm2.add(cbbPosition, "14, 4, 3, 1, fill, center");
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(Color.RED);
		panelForm2.add(lbl, "18, 4");
		
		lblContract = new JLabel("Hợp đồng :");
		lblContract.setFont(UtilityGUI.fontLblForm);
		panelForm2.add(lblContract, "2, 6, left, center");
		cbbContract = UtilityGUI.getComboBox("Contract", 0,"");
		panelForm2.add(cbbContract, "4, 6, 3, 1, fill, center");
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(Color.RED);
		panelForm2.add(lbl, "8, 6");
		
		lblDateStart = new JLabel("Bắt đầu làm việc :");
		lblDateStart.setFont(UtilityGUI.fontLblForm);
		panelForm2.add(lblDateStart, "2, 8, left, center");
		dateStart = new JDateChooser();
		dateStart.setLocale(new Locale("vi"));
		Calendar cal = Calendar.getInstance();
		cal.setTime(Main.fullday);
		cal.add(Calendar.DATE, maxDayStart);
		Date maxDateStart = cal.getTime();
		dateStart.setMaxSelectableDate(maxDateStart);
		Date minDateStart = null;
		try {
			minDateStart = (Date) formatter.parseObject(minStrDateStart);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		dateStart.setMinSelectableDate(minDateStart);
		dateStart.setDateFormatString(Main.strFormatDate);
		dateStart.setFont(new Font("Tahoma", Font.BOLD, 11));
		panelForm2.add(dateStart, "4, 8, 3, 1, fill, center");
		lbl = new JLabel("*");
		lbl.setFont(UtilityGUI.fontNote);
		lbl.setForeground(Color.RED);
		panelForm2.add(lbl, "8, 8");
		
		lblDiplomas = new JLabel("Bằng cấp :");
		lblDiplomas.setFont(UtilityGUI.fontLblForm);
		panelForm2.add(lblDiplomas, "2, 10, left, center");
		cbbDiplomas = UtilityGUI.getComboBox("Diplomas", 0,"");
		panelForm2.add(cbbDiplomas, "4, 10, 3, 1, fill, center");
		
		lbl = new JLabel("*");
		lbl.setForeground(Color.RED);
		lbl.setFont(new Font("Tahoma", Font.ITALIC, 11));
		panelForm2.add(lbl, "8, 10");
		
		lblSpec = new JLabel("Chuyên ngành :");
		lblSpec.setFont(UtilityGUI.fontLblForm);
		panelForm2.add(lblSpec, "12, 10, left, center");
		cbbSpec = UtilityGUI.getComboBox("Specialized", 0,"");
		panelForm2.add(cbbSpec, "14, 10, 3, 1, fill, center");
		
		lbl = new JLabel("*");
		lbl.setForeground(Color.RED);
		lbl.setFont(new Font("Tahoma", Font.ITALIC, 11));
		panelForm2.add(lbl, "18, 10");
		
		panelBtn = new JPanel();
		GridBagConstraints gbc_panelBtn = new GridBagConstraints();
		gbc_panelBtn.insets = new Insets(15, 5, 5, 15);
		gbc_panelBtn.fill = GridBagConstraints.BOTH;
		gbc_panelBtn.gridx = 0;
		gbc_panelBtn.gridy = 4;
		panelMain.add(panelBtn, gbc_panelBtn);
		GridBagLayout gbl_panelBtn = new GridBagLayout();
		gbl_panelBtn.columnWidths = new int[]{0, 0};
		gbl_panelBtn.rowHeights = new int[]{0, 0};
		gbl_panelBtn.columnWeights = new double[]{0.5, 0.5};
		gbl_panelBtn.rowWeights = new double[]{0.5, 0.5};
		panelBtn.setLayout(gbl_panelBtn);
		
		btnSave = new JButton(UtilityGUI.btnSave);
		btnSave.setMnemonic('L');
		btnSave.setFont(UtilityGUI.fontBtnForm);
		btnSave.addActionListener(this);
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.insets = new Insets(0, 0, 0, 5);
		gbc_btnSave.fill = GridBagConstraints.VERTICAL;
		gbc_btnSave.anchor = GridBagConstraints.EAST;
		gbc_btnSave.gridx = 0;
		gbc_btnSave.gridy = 0;
		panelBtn.add(btnSave, gbc_btnSave);
		
		btnCancel = new JButton(UtilityGUI.btnCancel);
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		btnCancel.setMnemonic('T');
		btnCancel.addActionListener(this);
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.insets = new Insets(0, 5, 0, 0);
		gbc_btnCancel.anchor = GridBagConstraints.WEST;
		gbc_btnCancel.fill = GridBagConstraints.VERTICAL;
		gbc_btnCancel.gridx = 1;
		gbc_btnCancel.gridy = 0;
		panelBtn.add(btnCancel, gbc_btnCancel);
		
		lblNote = new JLabel(UtilityGUI.noticeForm);
		lblNote.setFont(new Font("Veranda", Font.ITALIC, 10));
		lblNote.setForeground(Color.red);
		GridBagConstraints gbc_lblNote = new GridBagConstraints();
		gbc_lblNote.gridx = 0;
		gbc_lblNote.gridy = 1;
		gbc_lblNote.gridwidth = 2;
		gbc_lblNote.insets = new Insets(8, 0, 0, 15);
		gbc_lblNote.fill = GridBagConstraints.VERTICAL;
		gbc_lblNote.anchor = GridBagConstraints.EAST;
		panelBtn.add(lblNote, gbc_lblNote);
		
		scrollMain = new JScrollPane();
		scrollMain.setBorder(new EmptyBorder(0, 0, 0, 0));
		scrollMain.setViewportView(panelMain);
		this.add(scrollMain, BorderLayout.CENTER);
		
		groupTxt.add(txtFName);
		groupTxt.add(txtLName);
		groupTxt.add(txtCMTND);
		groupTxt.add(txtHomeTown);
		groupTxt.add(txtCurrent);
		groupTxt.add(txtPhone);
		groupTxt.add(txtMobile);
		groupTxt.add(txtEmail);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == cbbProvinceHT){
	    	if(cbbProvinceHT.getSelectedIndex()==0){
	    		cbbDistrictHT.setSelectedIndex(0);
	    		cbbDistrictHT.setEnabled(false);
	    		return;
	    	}	    	
	    	provinceHTId = Integer.parseInt(((KeyValue)cbbProvinceHT.getSelectedItem()).getKey()); 
	    	cbbDistrictHT.setVisible(false);
	    	cbbDistrictHT = (JComboBox) UtilityGUI.getComboBox("District", provinceHTId,"");
	    	cbbDistrictHT.addActionListener(this);
	    	panelForm1.add(cbbDistrictHT, "24, 6, 3, 1, fill, center");
		}else if(e.getSource() == cbbDistrictHT){
	    	if(cbbDistrictHT.getSelectedIndex()==0){
	    		return;
	    	}
	    	districtHTId = Integer.parseInt(((KeyValue)cbbDistrictHT.getSelectedItem()).getKey()); 
		}else if(e.getSource() == cbbProvinceCur){
			if(cbbProvinceCur.getSelectedIndex()==0){
				cbbDistrictCur.setSelectedIndex(0);
				cbbDistrictCur.setEnabled(false);
	    		return;
	    	} 
	    	provinceCurId = Integer.parseInt(((KeyValue)cbbProvinceCur.getSelectedItem()).getKey()); 
	    	cbbDistrictCur.setVisible(false);
	    	cbbDistrictCur = (JComboBox) UtilityGUI.getComboBox("District", provinceCurId,"");
	    	cbbDistrictCur.addActionListener(this);
	    	panelForm1.add(cbbDistrictCur, "24, 8, 3, 1, fill, default");
		}else if(e.getSource() == cbbDistrictCur){
			if(cbbDistrictCur.getSelectedIndex()==0){
	    		return;
	    	}
	    	districtCurId = Integer.parseInt(((KeyValue)cbbDistrictCur.getSelectedItem()).getKey());
		}
		
		
		//	Upload Img		
		if(e.getActionCommand().equals("SelectFile")){
			fileChooser = new JFileChooser();
			fileChooser.addChoosableFileFilter(new ImgFilter());
			fileChooser.setAcceptAllFileFilterUsed(false);
			fileChooser.setAccessory(new ImagePreview(fileChooser));
	        int returnValue = fileChooser.showOpenDialog(null);
	        if (returnValue == JFileChooser.APPROVE_OPTION) {
	        	selectedFile = fileChooser.getSelectedFile();
				resultSelectFile = CheckImg.getFormatInFile(selectedFile);
				if(resultSelectFile == null){
					JOptionPane.showMessageDialog(null, UtilityGUI.errorImg,
									UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
					return;
				}else{
					fileNameChooser = selectedFile.getName();
					txtPathImg.setText(fileNameChooser);
					btnChooserFile.setText(UtilityGUI.btnUpload);
					btnChooserFile.removeActionListener(this);
					btnChooserFile.setActionCommand("Upload");
					btnChooserFile.addActionListener(this);
				}
	        }
		}
		
		if(e.getActionCommand().equals("Upload")){
			boolean flag;
			Loading loading = new Loading("Upload...");
			try {				
				loading.open(120000);
				is = new FileInputStream(selectedFile);
				fileName = StringUtil.getFileWithoutExtension(fileNameChooser);
				iis = ImageIO.createImageInputStream(is);
				flag = UploadImg.upload(iis, fileName);
				fileName = UploadImg.fn;
			} catch (Exception e2) {
				e2.printStackTrace();
				flag = false;
			}
			if(flag == true){
				loading.close();
				JOptionPane.showMessageDialog(null, UtilityGUI.successUpload, 
								UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
				btnChooserFile.setText(UtilityGUI.btnDeleteFile);
				btnChooserFile.removeActionListener(this);
				btnChooserFile.setActionCommand("Delete");
				btnChooserFile.addActionListener(this);
			}else{
				JOptionPane.showMessageDialog(null, UtilityGUI.errorAction, 
								UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				return;
			}
		}
		
		if(e.getActionCommand().equals("Delete")){
			if(UploadImg.delete(fileName)){
				fileNameChooser = "";
				fileName = "";
				txtPathImg.setText("");
				btnChooserFile.setText(UtilityGUI.btnSelectFile);
				btnChooserFile.removeActionListener(this);				
				btnChooserFile.setActionCommand("SelectFile");
				btnChooserFile.addActionListener(this);
			}else{
				JOptionPane.showMessageDialog(null, UtilityGUI.errorAction, 
								UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				return;
			}
		}
		
		
		//	Valid data		
		if(e.getSource() == btnSave){
			if(btnChooserFile.getActionCommand().equals("Upload")){
				int option = JOptionPane.showConfirmDialog(null, "Chưa upload file ảnh. Bạn có muốn upload ảnh?",
						UtilityGUI.titleAlert, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
				if(option == JOptionPane.OK_OPTION){
					btnChooserFile.doClick();
					
				}else{
					fileNameChooser = "";
					fileName = "";
					txtPathImg.setText("");
					btnChooserFile.setText(UtilityGUI.btnSelectFile);
					btnChooserFile.removeActionListener(this);				
					btnChooserFile.setActionCommand("SelectFile");
					btnChooserFile.addActionListener(this);
				}
			}
			if(!StringUtil.isValidName(txtFName.getText(),50, 2)){
				JOptionPane.showMessageDialog(null, 
						UtilityGUI.errorValidName(StringUtil.removeColon(lblFName.getText()), 50, 2),
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				txtFName.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				txtFName.setBackground(UtilityGUI.txtFocusError);				
				return;
				
		   }else if(!StringUtil.isValidName(txtLName.getText(),50, 1)){
				JOptionPane.showMessageDialog(null, 
						UtilityGUI.errorValidName(StringUtil.removeColon(lblLName.getText()), 50, 1),
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				txtLName.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				txtLName.setBackground(UtilityGUI.txtFocusError);				
				return;
				
		   }else if(dateBirthday.getDate() == null){
				dateBirthday.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				return;
				
			}else if(!StringUtil.isValidCMTND(txtCMTND.getText())){
				JOptionPane.showMessageDialog(null, 
						UtilityGUI.errorValid(StringUtil.removeColon(lblCMTND.getText())),
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				txtCMTND.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				txtCMTND.setBackground(UtilityGUI.txtFocusError);				
				return;
				
			}else if(cbbProvinceHT.getSelectedIndex() == 0){
				cbbProvinceHT.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				return;
				
			}else if(cbbDistrictHT.getSelectedIndex() == 0){
				cbbDistrictHT.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				return;
				
			}else if(!StringUtil.isValidTxtVN(txtCurrent.getText(), 250, 3)){
				JOptionPane.showMessageDialog(null, 
						UtilityGUI.errorValidName(StringUtil.removeColon(lblCurrent.getText()), 250, 3),
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				txtCurrent.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				txtCurrent.setBackground(UtilityGUI.txtFocusError);				
				return;
				
			}else if(cbbProvinceCur.getSelectedIndex() == 0){
				cbbProvinceCur.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				return;
				
			}else if(cbbDistrictCur.getSelectedIndex() == 0){
				cbbDistrictCur.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				return;
				
			}else if(txtPhone.getText().length()>0 && !StringUtil.isValidPhone(txtPhone.getText())){
				JOptionPane.showMessageDialog(null, 
						UtilityGUI.errorValid(StringUtil.removeColon(lblPhone.getText())),
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				txtPhone.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				txtPhone.setBackground(UtilityGUI.txtFocusError);				
				return;
			
			}else if(!StringUtil.isValidMobile(txtMobile.getText())){
				JOptionPane.showMessageDialog(null, 
						UtilityGUI.errorValid(StringUtil.removeColon(lblMobile.getText())),
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				txtMobile.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				txtMobile.setBackground(UtilityGUI.txtFocusError);				
				return;
		
			}else if(txtEmail.getText().length()> 0 && !StringUtil.isValidEmail(txtEmail.getText())){
				JOptionPane.showMessageDialog(null, 
						UtilityGUI.errorValid(StringUtil.removeColon(lblEmail.getText())),
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				txtEmail.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				txtEmail.setBackground(UtilityGUI.txtFocusError);
				return;
		
			}else if(cbbDep.getSelectedIndex() == 0){
				cbbDep.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				return;
		
			}else if(cbbEmT.getSelectedIndex() == 0){
				cbbEmT.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				return;
				
			}else if(cbbPosition.getSelectedIndex() == 0){
				cbbPosition.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				return;
				
			}else if(cbbContract.getSelectedIndex() == 0){
				cbbContract.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				return;
			
			}else if(dateStart.getDate() == null){
				dateStart.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				return;
			
			}else if(cbbDiplomas.getSelectedIndex() == 0){
				cbbDiplomas.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				return;
				
			}else if(cbbSpec.getSelectedIndex() == 0){
				cbbSpec.requestFocusInWindow();
				for(int i = 0; i <groupTxt.size(); i++){
					groupTxt.get(i).setBackground(UtilityGUI.txtBlurError);
				}
				return;
				
			}
			String dateBirth = Main.sdfDate.format(dateBirthday.getDate());
			String start = Main.sdfDate.format(dateStart.getDate());
			boolean sex = true;
			if(rdbtnNu.isSelected()){
				sex = false;
			}
			Employee em = new Employee();
			em.setEmFirstName(txtFName.getText());
			em.setEmLastName(txtLName.getText());
			em.setEmFullName(txtFName.getText(), txtLName.getText());
			em.setSex(sex);
			em.setEmBirthDay(dateBirth);
			em.setEmCMTND(txtCMTND.getText());
			em.setEmHomeTownAdd(txtHomeTown.getText());
			em.setEmHomeTown(districtHTId);
			em.setEmCurrentAdd(txtCurrent.getText());
			em.setEmCurrentCity(districtCurId);
			em.setEmPhone(txtPhone.getText());
			em.setEmMobile(txtMobile.getText());
			em.setEmEmail(txtEmail.getText());
			em.setEmImage(fileName+"."+UploadImg.typeImg);
			em.setEmDep(Integer.parseInt(((KeyValue)cbbDep.getSelectedItem()).getKey()));
			em.setEmEmT(Integer.parseInt(((KeyValue)cbbEmT.getSelectedItem()).getKey()));
			em.setEmPos(Integer.parseInt(((KeyValue)cbbPosition.getSelectedItem()).getKey()));
			em.setEmContract(Integer.parseInt(((KeyValue)cbbContract.getSelectedItem()).getKey()));
			em.setEmDateStart(start);
			em.setEmDiplomas(Integer.parseInt(((KeyValue)cbbDiplomas.getSelectedItem()).getKey()));
			em.setEmSpecialized(Integer.parseInt(((KeyValue)cbbSpec.getSelectedItem()).getKey()));
			if(Main.actionEm.insertEmployee(em, Main.userId)){
				JOptionPane.showMessageDialog(null, UtilityGUI.successAction,
							UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
				this.setVisible(false);
				Main.jTabbedPane.remove(this);
				fileName = "";
				
				int indexComView = Main.jTabbedPane.positionTab(UtilityGUI.tabComView);
				if(indexComView > -1){
					Main.jTabbedPane.remove(indexComView);
					Main.jTabbedPane.addTab(new Company_View(), UtilityGUI.tabComView, indexComView);
				}
				
				int indexDepList = Main.jTabbedPane.positionTab(UtilityGUI.tabDepList);
				if(indexDepList > -1){
					Dep_List.tblDepList.setVisible(false);
					Dep_List.tblDepList = UtilityGUI.getTable("DepartmentCom", 1, "Dep_Id", true);
					Dep_List.tblDepList.addMouseListener(new Dep_List.Mouse_tblDep());
					Dep_List.scrollPane.setViewportView(Dep_List.tblDepList);
					Dep_List.cbbPage.setSelectedIndex(0);
				}
				
				int indexDepView = Main.jTabbedPane.positionTab(UtilityGUI.tabDepInfo);
				if(indexDepView > -1 && Dep_View.depId == em.getEmDep()){
					Main.jTabbedPane.remove(indexDepView);
					Main.jTabbedPane.addTab(new Dep_View(em.getEmDep()), UtilityGUI.tabDepInfo, indexDepView);
				}
				
				int indexEmDep_List = Main.jTabbedPane.positionTab(UtilityGUI.tabEmDepList);
				if(indexEmDep_List > -1 && EmDep_List.depId == em.getEmDep()){
					EmDep_List.tblEmDepList.setVisible(false);
					EmDep_List.tblEmDepList = UtilityGUI.createTblListEmDep(em.getEmDep(), true, false, 1, "Em_Id", true);
					EmDep_List.tblEmDepList.addMouseListener(new EmDep_List.Mouse_tblEmDep());
					EmDep_List.scrollPane.setViewportView(EmDep_List.tblEmDepList);
					EmDep_List.cbbPage.setSelectedIndex(0);
				}
				
				int indexEmCom_List = Main.jTabbedPane.positionTab(UtilityGUI.tabEmComList);
				int totalPage = Paging.getTotalPage("vi_EMInfo");
				if(indexEmCom_List == -1){
					Main.jTabbedPane.addTab(new EmCom_List(totalPage), UtilityGUI.tabEmComList);
					Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
					EmCom_List.cbbPage.setSelectedIndex(totalPage-1);
				}else{
					EmCom_List.tblEmComList.setVisible(false);
					EmCom_List.tblEmComList = UtilityGUI.getTable("EmployeeInCom", totalPage, "Em_Id", true);
					EmCom_List.tblEmComList.addMouseListener(new EmCom_List.Mouse_tblEmCom());
					EmCom_List.scrollPane.setViewportView(EmCom_List.tblEmComList);
					EmCom_List.cbbPage.setSelectedIndex(totalPage-1);
					Main.jTabbedPane.setSelectedIndex(indexEmCom_List);
				}
				
			}else{
				JOptionPane.showMessageDialog(null, UtilityGUI.errorAction, 
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				return;
			}
			
		}else if(e.getSource() == btnCancel){
			if(UploadImg.delete(fileName)){
				this.setVisible(false);
				Main.jTabbedPane.remove(this);
				fileName = "";
			}else{
				JOptionPane.showMessageDialog(null, UtilityGUI.errorAction, 
								UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				return;
			}
		}		
	}

}
